local Filters = {
    alterguardian_phase1 = true,
    alterguardian_phase2 = true,
    alterguardian_phase3 = true,
    crabking = true,
    stalker_atrium = true,
}

AddPrefabPostInit("kukude", function(inst)
    if not TheWorld.ismastersim then return end 

    inst.components.kkd_joinkill:SetTargetDeathFn("nofadeout", function(inst, target)
        if target.components.health then 
            -- target.persists = false
            -- target:AddTag("kkd_corpse")
            -- target.components.health.nofadeout = true 
            -- target.erode_task = target:DoTaskInTime(TUNING.MOD_KUKUDE.DISSECT.ERODE_TIME, ErodeAway)

            -- 这里筛选掉不解剖的条件
            local allow_to_continue = true
            if target.prefab and target.prefab == "klaus" then
                if not target:IsUnchained() then
                    allow_to_continue = false
                end
            end
            if target.prefab and Filters[target.prefab] then
                allow_to_continue = false
            end

            if allow_to_continue then
                target:AddTag("kkd_corpse")
                target:RemoveTag("NOCLICK")
                target.AddTag = function(...) end
                target.kkd_deathtime = GetTime()
                --[[local old_dotask = target.DoTaskInTime
                print("printing target.sg.sg.states.death.timeline:")
                print(target)
                for i, n in pairs(target.sg.sg.states.death.timeline) do 
                    print(i)
                    for k, v in pairs(n) do
                        print(k)
                        print(v)
                    end
                end

                target.DoTaskInTime = function(self, t, fn, ...)
                    if fn == ErodeAway then
                        t = TUNING.MOD_KUKUDE.DISSECT.ERODE_TIME 
                    end
                    old_dotask(self, t, fn, ...)
                end]]
                --[[target.taskpeirod_kkd_keep_body_click = target:DoPeriodicTask(0, function()
                    if target and target:HasTag("NOCLICK") then
                        target:RemoveTag("NOCLICK")
                    end
                end)
                target:DoTaskInTime(TUNING.MOD_KUKUDE.DISSECT.ERODE_TIME, function()
                    if target then
                        target:AddTag("NOCLICK")
                    end
                    if target.taskpeirod_kkd_keep_body_click then
                        target.taskpeirod_kkd_keep_body_click:Cancel()
                        target.taskpeirod_kkd_keep_body_click = nil
                    end
                end)]]
            end
        end
    end)
end)

local old_erode = GLOBAL.ErodeAway
GLOBAL.ErodeAway = function(tar)
    if tar.kkd_deathtime then 
        if tar.kkd_deathtime == true then
            tar.kkd_deathtime = GetTime()
            old_erode(tar)
            return
        else
            local time = TUNING.MOD_KUKUDE.DISSECT.ERODE_TIME - GetTime() + tar.kkd_deathtime
            if time > 0 then
                tar:DoTaskInTime(time, old_erode)
                return 
            end
        end
    end
    old_erode(tar)
end